﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using VPBS.Informix.DAL;
using VPBS.SqlClient.DAL;

namespace TempEEWindowsService
{
    public partial class TempEEWindowsService : ServiceBase
    {
        System.Timers.Timer timer;
        EventLog eventLog;
        public TempEEWindowsService()
        {
            InitializeComponent();
            // Reads settings from app.config
            this.ServiceName = ConfigurationSettings.AppSettings["serviceName"];
            string eventLogName = ConfigurationSettings.AppSettings["eventLogName"];
            string eventLogSource = ConfigurationSettings.AppSettings["eventLogSource"];

            // Initializes eventlog
            if (!EventLog.SourceExists(eventLogSource))
                EventLog.CreateEventSource(eventLogSource, eventLogName);

            eventLog = new EventLog();
            ((System.ComponentModel.ISupportInitialize)(eventLog)).BeginInit();

            eventLog.Source = eventLogSource;

            // Registers the application
            eventLog.Log = eventLogName;

            ((System.ComponentModel.ISupportInitialize)(eventLog)).EndInit();

            //CaptureAccountTab_CB();
        }

        protected override void OnStart(string[] args)
        {
            eventLog.WriteEntry("+++++++++++++ START SMS SERVICE +++++++++++++", EventLogEntryType.Warning);

            // Runs the process every X minutes
            int interval = Int32.Parse(ConfigurationSettings.AppSettings["interval"]) * 1000;
            timer = new System.Timers.Timer();
            timer.Interval = interval;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(CaptureAccountTab_CB);
            timer.Start();
        }

        protected void CaptureAccountTab_CB(object obj, System.Timers.ElapsedEventArgs e)
        {
            int startHour = Int32.Parse(ConfigurationSettings.AppSettings["startTime"]);
            int stopHour = Int32.Parse(ConfigurationSettings.AppSettings["stopTime"]);
            DateTime now = DateTime.Now;
            string strMinute = now.Minute.ToString();
            if (now.Minute < 10) strMinute = "0" + now.Minute.ToString();
            int Time = int.Parse(now.Hour.ToString() + strMinute);


            string sSQLn = "select * from tmpeeAccountTab_CB where INSERTDATE_USER is not null and datediff(day, '" +
                           now.ToString("yyyy/MM/dd") + "', EffDate)=0 and datediff(day, InsertDate, '" +
                           now.ToString("yyyy/MM/dd") + "')=0";
            DataSet NightOrdern = new TnTempEE().returnDataSet(sSQLn);

            if ((Time >= 0 && Time < startHour) || (Time >= stopHour && Time <= 2300) ||
                ThdHolydayDateDAL.CheckOfHolydayDate() == true || (NightOrdern != null && NightOrdern.Tables[0].Rows.Count > 0))
            {
                return;
            }

            string sSQL = "select * from tmpeeAccountTab_CB where INSERTDATE_USER is null and datediff(day, '" +
                          now.ToString("yyyy/MM/dd") + "', EffDate)=0 and datediff(day, insertdate, '" +
                          now.ToString("yyyy/MM/dd") + "')>0";
            DataSet NightOrder = new TnTempEE().returnDataSet(sSQL);
            if (NightOrder != null && NightOrder.Tables[0].Rows.Count>0)
            {
                for (int i = 0; i < NightOrder.Tables[0].Rows.Count; i++)
                {
                    Update(NightOrder.Tables[0].Rows[i]["CUST_ID"].ToString().Trim(), now, NightOrder.Tables[0].Rows[i]["ACCT_ID"].ToString());
                }
                return;
            }
            return;
        }

        protected void Update(string Account, DateTime InsertDate, string ACCT_ID)
        {
            DataSet dataSet = new WebService.Service().Get_ACCOUNTTAB_CB(Account);
            if (dataSet != null && dataSet.Tables[0].Rows.Count > 0)
            {
                string INSERTDATE,
                       TRADERID,
                       CREDITLIMIT,
                       BUYCREDIT,
                       CUSTTYPE,
                       CREDITTYPE,
                       T_BUY,
                       T_SELL,
                       SELLSHORTCREDIT,
                       PC,
                       ASSET,
                       LIABILITY,
                       EQUITY,
                       CASH_BALANCE,
                       LMV,
                       COLLAT,
                       DEBT,
                       SMV,
                       MR,
                       BUYMR,
                       SELLMR,
                       EE,
                       PP,
                       CALL_MARGIN,
                       CALL_FORCE,
                       BRK_CALL_LMV,
                       BRK_CALL_SMV,
                       BRK_SELL_LMV,
                       BRK_SELL_SMV,
                       ACTION,
                       BUYUNMATCH,
                       SELLUNMATCH,
                       AR,
                       AP,
                       AR_T1,
                       AP_T1,
                       AR_T2,
                       AP_T2,
                       WITHDRAWAL,
                       LMV_HAIRCUT,
                       EQUITY_HAIRCUT,
                       EE_1,
                       EE_50,
                       EE_60,
                       EE_70,
                       EE_MTM,
                       EE_MTM50,
                       EE_MTM60,
                       EE_MTM70,
                       UPDATEFLAG,
                       DELFLAG,
                       ADV_WITHDRAW,
                       RATIO;
                
                INSERTDATE = InsertDate.ToString();
                TRADERID = dataSet.Tables[0].Rows[0]["TRADERID"].ToString().Trim();
                CREDITLIMIT = dataSet.Tables[0].Rows[0]["CREDITLIMIT"].ToString().Trim();
                BUYCREDIT = dataSet.Tables[0].Rows[0]["BUYCREDIT"].ToString().Trim();
                CUSTTYPE = dataSet.Tables[0].Rows[0]["CUSTTYPE"].ToString().Trim();
                CREDITTYPE = dataSet.Tables[0].Rows[0]["CREDITTYPE"].ToString().Trim();
                T_BUY = dataSet.Tables[0].Rows[0]["T_BUY"].ToString().Trim();
                T_SELL = dataSet.Tables[0].Rows[0]["T_SELL"].ToString().Trim();
                SELLSHORTCREDIT = dataSet.Tables[0].Rows[0]["SELLSHORTCREDIT"].ToString().Trim();
                PC = dataSet.Tables[0].Rows[0]["PC"].ToString().Trim();
                ASSET = dataSet.Tables[0].Rows[0]["ASSET"].ToString().Trim();
                LIABILITY = dataSet.Tables[0].Rows[0]["LIABILITY"].ToString().Trim();
                EQUITY = dataSet.Tables[0].Rows[0]["EQUITY"].ToString().Trim();
                CASH_BALANCE = dataSet.Tables[0].Rows[0]["CASH_BALANCE"].ToString().Trim();
                LMV = dataSet.Tables[0].Rows[0]["LMV"].ToString().Trim();
                COLLAT = dataSet.Tables[0].Rows[0]["COLLAT"].ToString().Trim();
                DEBT = dataSet.Tables[0].Rows[0]["DEBT"].ToString().Trim();
                SMV = dataSet.Tables[0].Rows[0]["SMV"].ToString().Trim();
                MR = dataSet.Tables[0].Rows[0]["MR"].ToString().Trim();
                BUYMR = dataSet.Tables[0].Rows[0]["BUYMR"].ToString().Trim();
                SELLMR = dataSet.Tables[0].Rows[0]["SELLMR"].ToString().Trim();
                EE = dataSet.Tables[0].Rows[0]["EE"].ToString().Trim();
                PP = dataSet.Tables[0].Rows[0]["PP"].ToString().Trim();
                CALL_MARGIN = dataSet.Tables[0].Rows[0]["CALL_MARGIN"].ToString().Trim();
                CALL_FORCE = dataSet.Tables[0].Rows[0]["CALL_FORCE"].ToString().Trim();
                BRK_CALL_LMV = dataSet.Tables[0].Rows[0]["BRK_CALL_LMV"].ToString().Trim();
                BRK_CALL_SMV = dataSet.Tables[0].Rows[0]["BRK_CALL_SMV"].ToString().Trim();
                BRK_SELL_LMV = dataSet.Tables[0].Rows[0]["BRK_SELL_LMV"].ToString().Trim();
                BRK_SELL_SMV = dataSet.Tables[0].Rows[0]["BRK_SELL_SMV"].ToString().Trim();
                ACTION = dataSet.Tables[0].Rows[0]["ACTION"].ToString().Trim();
                BUYUNMATCH = dataSet.Tables[0].Rows[0]["BUYUNMATCH"].ToString().Trim();
                SELLUNMATCH = dataSet.Tables[0].Rows[0]["SELLUNMATCH"].ToString().Trim();
                AR = dataSet.Tables[0].Rows[0]["AR"].ToString().Trim();
                AP = dataSet.Tables[0].Rows[0]["AP"].ToString().Trim();
                AR_T1 = dataSet.Tables[0].Rows[0]["AR_T1"].ToString().Trim();
                AP_T1 = dataSet.Tables[0].Rows[0]["AP_T1"].ToString().Trim();
                AR_T2 = dataSet.Tables[0].Rows[0]["AR_T2"].ToString().Trim();
                AP_T2 = dataSet.Tables[0].Rows[0]["AP_T2"].ToString().Trim();
                WITHDRAWAL = dataSet.Tables[0].Rows[0]["WITHDRAWAL"].ToString().Trim();
                LMV_HAIRCUT = dataSet.Tables[0].Rows[0]["LMV_HAIRCUT"].ToString().Trim();
                EQUITY_HAIRCUT = dataSet.Tables[0].Rows[0]["EQUITY_HAIRCUT"].ToString().Trim();
                EE_1 = dataSet.Tables[0].Rows[0]["EE_1"].ToString().Trim();
                EE_50 = dataSet.Tables[0].Rows[0]["EE_50"].ToString().Trim();
                EE_60 = dataSet.Tables[0].Rows[0]["EE_60"].ToString().Trim();
                EE_70 = dataSet.Tables[0].Rows[0]["EE_70"].ToString().Trim();
                EE_MTM = dataSet.Tables[0].Rows[0]["EE_MTM"].ToString().Trim();
                EE_MTM50 = dataSet.Tables[0].Rows[0]["EE_MTM50"].ToString().Trim();
                EE_MTM60 = dataSet.Tables[0].Rows[0]["EE_MTM60"].ToString().Trim();
                EE_MTM70 = dataSet.Tables[0].Rows[0]["EE_MTM70"].ToString().Trim();
                UPDATEFLAG = dataSet.Tables[0].Rows[0]["UPDATEFLAG"].ToString().Trim();
                DELFLAG = dataSet.Tables[0].Rows[0]["DELFLAG"].ToString().Trim();
                ADV_WITHDRAW = dataSet.Tables[0].Rows[0]["ADV_WITHDRAW"].ToString().Trim();
                RATIO = dataSet.Tables[0].Rows[0]["RATIO"].ToString().Trim();
                
                bool re = new TnTempEE().Update(ACCT_ID, INSERTDATE, TRADERID, CREDITLIMIT, BUYCREDIT,
                                                     CUSTTYPE, CREDITTYPE, T_BUY, T_SELL, SELLSHORTCREDIT, PC, ASSET,
                                                     LIABILITY, EQUITY, CASH_BALANCE, LMV, COLLAT, DEBT, SMV, MR, BUYMR,
                                                     SELLMR, EE, PP, CALL_MARGIN, CALL_FORCE, BRK_CALL_LMV, BRK_CALL_SMV,
                                                     BRK_SELL_LMV, BRK_SELL_SMV, ACTION, BUYUNMATCH, SELLUNMATCH, AR, AP,
                                                     AR_T1, AP_T1, AR_T2, AP_T2, WITHDRAWAL, LMV_HAIRCUT, EQUITY_HAIRCUT,
                                                     EE_1, EE_50, EE_60, EE_70, EE_MTM, EE_MTM50, EE_MTM60, EE_MTM70,
                                                     UPDATEFLAG, DELFLAG, ADV_WITHDRAW, RATIO);
            }
        }

        protected override void OnStop()
        {
            eventLog.WriteEntry("+++++++++++++ STOP SMS SERVICE +++++++++++++", EventLogEntryType.Warning);
        }
    }
}
